<template>
  <div class="frames" id="frames">
    <div class="sticky" id="sticky-ct">
      <canvas id="zhuban" width="1920" height="1080" style="transform: translate(-20%)"></canvas>
      <div class="info" :class="{ show: index>30 }">
        <div class="tip">
          <div class="line"></div>
          <div class="text">1T内存大空间</div>
        </div>
        <div class="tip">
          <div class="line"></div>
          <div class="text">i5主板超强性能</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
export default {
  mounted() {
    this.init();
  },
  data() {
    return {
      index: null,
    };
  },
  methods: {
    init() {
      let that = this
      $("#zhuban").width(1900);

      $(window).on("scroll", function() {
        let frames = document.getElementById("frames");
        let stickyCt = document.getElementById("sticky-ct");

        let scrolled = stickyCt.offsetTop / frames.offsetHeight;

        let frame = Math.ceil(scrolled * 65);
        changeFrame(frame);
      });

      const loader = new PxLoader();
      const images = [];

      for (let i = 0; i < 66; i++) {
        images[i] = loader.addImage(require(`../assets/frames/zb${i + 1}.png`));
      }

      loader.addCompletionListener(function() {
        let context = $("#zhuban")[0].getContext("2d");

        $("body").addClass("loaded");

        context.drawImage(images[0], 0, 0, 1920, 1080);
      });

      loader.start();

      function changeFrame(frame) {
        let index = frame - 1;
        that.index = index;
        if (index < 0) index = 0;
        if (index > 65) index = 65;

        let context = $("#zhuban")[0].getContext("2d");
        context.clearRect(0, 0, 1920, 1080);
        context.drawImage(images[index], 0, 0, 1920, 1080);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.frames {
  height: 5000px;
  position: relative;
  z-index: 11;
}
.sticky {
  position: sticky;
  top: 0;
}
.info {
  position: absolute;
  top: 50%;
  left: 70%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  transform: translate(-80%, -50%);
  height: 280px;
  transition: all 0.5s;
  opacity: 0;
  &.show {
    opacity: 1;
    transform: translate(-80%, -80%);
  }
  .tip {
    height: 47px;
    font-size: 36px;
    font-family: OPPOSans-M, OPPOSans;
    font-weight: normal;
    color: rgba(51, 51, 51, 1);
    line-height: 47px;
    letter-spacing: 2px;
    display: flex;
    align-items: center;
    .line {
      width: 500px;
      height: 6px;
      background: #13C2C2;
      margin-right: 30px;
      position: relative;
    }
    .line::before {
      content: '';
      width: 16px;
      height: 16px;
      background: #13C2C2;
      border-radius: 50%;
      top: 50%;
      left: 0;
      transform: translate(-50%, -50%);
      position: absolute;
    }
    .text {
      white-space: nowrap;
    }
  }
  .tip:nth-of-type(1) {
    .line {
      width: 600px;
    }
  }
}
</style>